#include "sendmessage.h"
#include "connect.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pgsql/libpq-fe.h>

 bool sendmessage(int toID, char* content){
    extern PGconn *conn;
	if(!conn) conn = dbconnect();
	char command[2048];
	sprintf(command, "insert into messages(fromid, toid, content) values (3, %d, $lddfg$%s$lddfg$);", toID, content);
	PGresult *res;
	res = PQexec(conn, command);
	if(PQresultStatus(res) != PGRES_COMMAND_OK){
		fprintf(stderr, "发送失败!\n");
		PQclear(res);
		return false;
	}
	else{
		printf("发送成功\tTO: %d\n\t\tcontent: %s\n", toID, content);
	}
	
	return true;
}
/*
int main(){
	char buffer[1024];
	int TO;
	while(1){
		printf("sendTO: ");
		scanf("%d", &TO);
		printf("content: ");
		scanf("%s", buffer);
		sendmessage(TO, buffer);
	}
	return 0;
}
*/
